﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using SalesSys.BLL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;
using SalesSys.Site.Models;
using SalesSys.WebCore;
using SalesSys.WebCore.Authorize;

namespace SalesSys.Site.Controllers.Api
{
    [RoutePrefix("api/Employee")]
    public class EmployeeController : BaseApiController
    {
        private EmployeeBLL bll = BllFactory.GetBll<EmployeeBLL>();


        /// <summary>
        /// 员工列表
        /// </summary>
        /// <param name="name">员工姓名</param>
        /// <param name="isLeave">是否离职</param>
        /// <param name="departmentId">部门编号</param>
        /// <param name="page">分页</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        [Route("List")]
        public ViewDto<IEnumerable<Employee>> Get(string name = null, bool? isLeave = null, Guid? departmentId = null, int page = 1, int rows = 10)
        {
            KeyValuePair<int, IEnumerable<Employee>> re = bll.Select(name, isLeave, departmentId, page, rows);

            var dto = new ViewDto<IEnumerable<Employee>>();
            dto.Data = re.Value;
            dto.Total = re.Key;
            dto.Page = page;
            dto.Rows = rows;
            return dto;
        }

        /// <summary>
        /// 员工详情
        /// </summary>
        /// <param name="id">员工编号</param>
        /// <returns></returns>
        [Route("Detail")]
        public Employee Get(Guid id)
        {
            var re = bll.Details(id);
            re.LoginPwd = null;
            return re;
        }

        /// <summary>
        /// 新建用户
        /// </summary>
        /// <param name="model">用户实体</param>
        public IResultState Post(EmployeeDto model)
        {
            if (!string.IsNullOrWhiteSpace(model.LoginName))
            {
                var vUser = bll.GetList(p => p.LoginName == model.LoginName);
                if (vUser.Length > 0)
                {
                    return ReState.ArgError.Create("登录名重复");
                }
            }
            model.CreateTime = DateTime.Now;
            var entity = model.ToEntity();
            entity.LoginPwd = model.LoginPwd;
            if (model.DepartmentId != null)
            {
                entity.Department = bll.GetDepartment(model.DepartmentId.Value);
            }

            entity.Creater = bll.Details(ClientHelper.ClientInfo.Id);
            bll.Create(entity);
            return ReState.Success.Create("创建成功");
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="model">用户实体</param>
        /// <returns></returns>
        public IResultState Put(EmployeeDto model)
        {

            if (!string.IsNullOrWhiteSpace(model.LoginName))
            {
                var vUser = bll.GetList(p => model.Id != p.Id && p.LoginName == model.LoginName);
                if (vUser.Length > 0)
                {
                    return ReState.ArgError.Create("登录名重复");
                }
            }
            var old = bll.Details(model.Id.Value);
            if (model.DepartmentId != null)
            {
                old.Department = bll.GetDepartment(model.DepartmentId.Value);
            }
            bll.Edit(model.ToEntity(old));
            return ReState.Success.Create("修改成功");
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="newPwd">新密码</param>
        /// <param name="oldPwd">原始密码</param>
        /// <returns></returns>
        [Route("SetPower")]
        public IResultState PutSetPower(string newPwd, string oldPwd)
        {
            var old = bll.Details(ClientHelper.ClientInfo.Id);
            if (old.LoginPwd != oldPwd || string.IsNullOrWhiteSpace(oldPwd))
            {
                return ReState.ArgError.Create("原密码错误");
            }
            old.LoginPwd = newPwd;
            bll.Edit(old);
            return ReState.Success.Create();
        }
        /// <summary>
        /// 登出
        /// </summary>
        [AllowAnonymous]
        [Route("OutLogin")]
        public void OutLogin()
        {
            ClientHelper.OutLogin();
        }
    }
}
